package com.cool.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.cool.handler.Handler;

/**
 * @author bian
 * @date 2021-02-09
 */
@Service
public class CommonService implements ApplicationContextAware {
    private List<Handler> handlerList = new ArrayList<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        handlerList = applicationContext.getBeansOfType(Handler.class).values().stream()
            .sorted(Comparator.comparing(Handler::order)).collect(Collectors.toList());
    }

    public void handle() {
        handlerList.forEach(Handler::run);
    }
}
